package com.jule.miaozhuapc.dao.impl;

import com.jule.miaozhuapc.dao.AdvertDao;
import com.jule.miaozhuapc.domain.WawaAdvert;
import org.springframework.stereotype.Repository;

import javax.annotation.sql.DataSourceDefinition;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class AdvertDAOImpl  implements AdvertDao{

    private EntityManager em;
    @PersistenceContext
    public void setEm(EntityManager em) {
        this.em = em;
    }

    private EntityManager em2;
    @PersistenceContext
    public void setEm2(EntityManager em2) {
        this.em2 = em2;
    }

    @Override
    public List<WawaAdvert> findbyType(int type) {
        // FIXME
        String hql = "from WawaAdvert where type=:mytype  and isDelete = 0  and now() BETWEEN startTime and endTime  order by addTime DESC ";
        List<WawaAdvert>  wawaAdverts = em.createQuery(hql)
                .setParameter("mytype", type)
                .getResultList();

        return wawaAdverts;
    }

    @Override
    public List<WawaAdvert> findAll() {
        String hql = "from WawaAdvert where isDelete = 0  and now() BETWEEN startTime and endTime  order by addTime DESC ";
        List<WawaAdvert>  wawaAdverts = em.createQuery(hql, WawaAdvert.class)
                .getResultList();

        return wawaAdverts;
    }
}
